# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# http://doc.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals
import random
from scrapy.downloadermiddlewares.httpproxy import HttpProxyMiddleware
from tmgood.settings import UAPOOL
from scrapy.downloadermiddlewares.useragent import UserAgentMiddleware

class IPPOOLS(HttpProxyMiddleware):
    def __init__(self,ip=''):
        print (666)
        self.ip = ip

    def process_request(self, request, spider):
        with open('tmgood/ippool.txt', 'r') as f:
            thisip = f.readlines()
        # print(thisip)
        # print(random.choice(thisip))
        thisip = random.choice(thisip)
        request.meta["proxy"]="http://"+thisip


class Uamid(UserAgentMiddleware):
    def __init__(self,ua=''):
        self.ua = ua

    def process_request(self, request, spider):
        thisuaa = random.choice(UAPOOL)
        # print(thisuaa)
        request.headers.setdefault('User-Agent',thisuaa)